package com.news.control;

import com.news.data.entity.Article;
import com.news.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * Author : xuzhiyong
 * Date : 2022/5/23 10:41
 */
@Controller
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @RequestMapping("/newsList")
    public String newsList(Model model) {
        System.out.println("查询所有news..");
        List<Article> articles = articleService.findAllArticle();
        model.addAttribute("articles", articles);
        return "news/newsList";
    }

    @RequestMapping("/toAddArticle")
    public String toAdd() {
        return "news/newsAdd";
    }

    @RequestMapping("/addArticle")
    public String add(Article article) {
        articleService.addArticle(article);
        return "redirect:/newsList";
    }

    @RequestMapping("/toEditArticle")
    public String toEdit(Model model, int id) {
        Article article = articleService.findArticleById(id);
        model.addAttribute("article",article);
        return "news/newsEdit";
    }

    @RequestMapping("/editArticle")
    public String edit(Article article) {
        articleService.updateArticle(article);
        return "redirect:/newsList";
    }

    @RequestMapping("/deleteArticle")
    public String delete(int id) {
        articleService.deleteArticleById(id);
        return "redirect:/newsList";
    }




}
